package com.jeff.android.lab.jetpack.fragment

import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import butterknife.BindView
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.google.android.material.tabs.TabLayoutMediator.*
import com.jeff.android.lab.BaseFragment
import com.jeff.android.lab.R

/**
 * @author: dingfeng
 * @date: 2021/3/2
 * @since: 1.0.0
 **/
class Fragment_1_3 : BaseFragment() {

    @BindView(R.id.tab_layout)
    lateinit var mTabLayout: TabLayout

    @BindView(R.id.view_pager)
    lateinit var mViewPager: ViewPager2

    override fun getRootLayout(): Int {
        return R.layout.jetpack_1_3_fragment
    }

    override fun initData() {
        val fragments = mutableListOf<Fragment>()

        fragments.add(FragmentC())
        fragments.add(FragmentD())

        val fragmentStateAdapter = FragmentStateAdapterImpl<Fragment>(childFragmentManager, lifecycle)
        fragmentStateAdapter.setItems(fragments, arrayOf("C", "D"))
        mViewPager.offscreenPageLimit = fragments.size -1
        mViewPager.adapter = fragmentStateAdapter

        val mediator = TabLayoutMediator(mTabLayout, mViewPager, TabConfigurationStrategy { tab, position -> tab.text = fragmentStateAdapter.getPageTitle(position) })
        mediator.attach()


//        childFragmentManager.beginTransaction()
//                .add(R.id.container1, FragmentA())
//                .commitAllowingStateLoss()
//
//        childFragmentManager.beginTransaction()
//                .add(R.id.container2, FragmentB())
//                .commitAllowingStateLoss()
    }

}